<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="LAFDC.DataAccess" %>
<%@ Import Namespace="LAFDC.Logging" %>
<script language="c#" runat="server">
    public static readonly String source = "RoutageResetAdhByPrevId.aspx";
    private String result = "";

    protected void Page_Load(object sender, EventArgs e) {
        FDCLogger.LogEvent("Preparing to reset routage for prev_id.", source, 4);

        FDCLogger.LogEvent("First check that a param prev_id was passed to the get query.", source, 4);
        String prev_id = String.Empty;
        prev_id = this.Request.QueryString.Get("prev_id");
        if ( prev_id != String.Empty) {
            try {
                Type t =prev_id.GetType(); // If prev_id was reset by the QueryString.Get() method GetType will throw.
                
                // If it doesn't we have the param and can work with it!               
                ConnectionManager cn = new ConnectionManager();
                SqlConnection conn = cn.GetSqlConnection();
                SqlCommand tsql = conn.CreateCommand();
                
                tsql.CommandType = CommandType.Text;
                tsql.CommandText = "set nocount off update adherent set journal = 0 where prev_id = '" + prev_id + "'";
                
                FDCLogger.LogEvent("Preparing to execute SQL statement: " + tsql.CommandText, source, 2);
                
                conn.Open();
                int r = tsql.ExecuteNonQuery();
                conn.Close();
                
                FDCLogger.LogEvent("SQL statement execution completed. Return code = " + r.ToString(), source, 2);

                if ( r == 1) {
                    result = "SUCCESS";
                }
                else {
                    result="ERROR_RESULT_NOT_ONE";
                }
            }
            catch {
                result="ERROR_PREV_ID_NULL";
            }
        }
        else {
            result="ERROR_PREV_ID_NULL";
        }
    }
</script>
<%Response.Write(result);%>